'use strict';

angular.module('app')
    .filter("highlight",['$sce','$log', function ($sce, $log) {
    var fn = function (text, search) {
        //$log.info("text: " + text);
        //$log.info("search: " + search);
        if (!search) {
            return $sce.trustAsHtml(text);
        }
        //text = encodeURI(text);
        var searchList = search.split(" ");
        var result = "";
        for (var i = 0; i < searchList.length; i++) {
            //var temp = encodeURI(searchList[i]);
            var temp = searchList[i];
            var regex = new RegExp(temp, 'gi');
            text = text.replace(temp, '<red-font>$&</red-font>');
        }

        try{
            //result = decodeURI(text);
            result = text;
        }catch (e){
            //$log.info("text: " + text);
        }

        //$log.info("result: " + result);
        return $sce.trustAsHtml(result);
    };
    return fn;
}]);